/**
* @file task_system.c
* @brief system task 
* @details background task
* @author huangbin
* @version V1.0.0
* @date 2025-04-25
* @copyright All documents are copyrighted by Shenzhen Xinlongwei Technology Co., LTD.
*            They may not be used for commercial purposes without permission
*/
#include "inc.h"



#if USE_WORK_LED == 1
#define WORD_LED_PORT	GPIOA
#define WORD_LED_PIN	GPIO_Pin_15
static uint16_t WorkStatus = 0;
void WorkLedInitIo(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

    GPIO_InitStructure.GPIO_Pin = WORD_LED_PIN;		 
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
    GPIO_Init(WORD_LED_PORT, &GPIO_InitStructure);

    GPIO_ResetBits(WORD_LED_PORT, WORD_LED_PIN);   // LED turn on
    DebugPrintf("[system]work led init!\r\n");
}
void WorkLedFlash(void)
{
    if(WorkStatus == 0)
        GPIO_SetBits(WORD_LED_PORT, WORD_LED_PIN);
    else
        GPIO_ResetBits(WORD_LED_PORT, WORD_LED_PIN);
    WorkStatus = !WorkStatus;
}
#else
    #define WorkLedInitIo()
    #define WorkLedFlash()
#endif

uint8_t taskSystemId;
void taskSystemInit(uint8_t task_id)
{
    taskSystemId = task_id;
    WorkLedInitIo();
    osalSetEvent( task_id, TASK_SYS_INIT_EVT);
}

uint32_t taskSystem( uint8_t task_id, uint32_t events )
{
    if ( events & TASK_SYS_TIMER_EVT )
    {
        return ( events ^ TASK_SYS_TIMER_EVT );
    }

    if ( events & TASK_SYS_SYSTEM_RUN_EVT ) {
        osalStartTimer(taskSystemId,TASK_SYS_SYSTEM_RUN_EVT,OSAL_TIMER_100MS*5);
        WorkLedFlash();

        DebugPrintf("[system] running! [%lld]\r\n", get_system_ms());
        return ( events ^ TASK_SYS_SYSTEM_RUN_EVT );
    }

    if ( events & TASK_SYS_INIT_EVT ) {
        osalStartTimer(taskSystemId,TASK_SYS_SYSTEM_RUN_EVT,OSAL_TIMER_100MS);
        return ( events ^ TASK_SYS_INIT_EVT );
    }
    return 0;
}

